﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Security.Policy;
using System.Threading.Tasks;
using System.Xml.Linq;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Model.UI;
using CoreShop.Repository;
using Newtonsoft.Json;
using SqlSugar;
using SqlSugar.Extensions;
using static CoreShop.Library.GlobalEnumVars;


namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class activitygoodsServices : Repository<activitygoods>, IactivitygoodsServices
    {
        #region 实现重写增删改查操作==========================================================

        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public bool InsertAsync(activitygoods entity)
        {
            return base.Change<activitygoods>().Insert(entity);
        }
        public bool InsertOrUpdate(List<activitygoods> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(activitygoods entity)
        {
            return base.Change<activitygoods>().Update(a => new activitygoods()
            {
                activityInfoId = entity.activityInfoId,
            }, a => a.activityInfoId == entity.activityInfoId);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<activitygoods> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<activitygoods>().Update(a => new activitygoods()
                {
                    activityInfoId = item.activityInfoId,
                }, a => a.activityInfoId == item.activityInfoId);
            }
            return returnKey;
        }
               
        #endregion

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<activitygoods> GetCaChe()
        {
            return base.GetList();
        }


        #endregion

        #region 促销计算(限时特价、限时折扣）
        /// <summary>
        /// 促销计算(限时特价、限时折扣）
        /// </summary>
        public ActivityDto SetOrderingDiscount(corecmsfoodordering ordering, long storeId, corecmsuser member)
        {
            ActivityDto activity = null;
            List<ActivityItemDto> activityList = new List<ActivityItemDto>();
            List<long> excGoodsSortIds = new List<long>();
            var goodsTypeIds = new corecmsfoodcategoryServices().GetParentIds(ordering.foodTypeId);
            try
            {
                var where0 = Expressionable.Create<activitymaster>();
                var where1 = Expressionable.Create<activitygoods, activitymaster>();
                var where2 = Expressionable.Create<activitysort, activitymaster>();
                var where3 = Expressionable.Create<activitygoodsexclude, activitymaster>();

                //1、活动类型
                //where0 = where0.And(p => p.activityTypeId == (int)GlobalEnumVars.ActivityEnum.特价 && p.activitySubTypeId == (int)GlobalEnumVars.ActivitySubType.限时折扣);
                where0 = where0.And(p => p.activityTypeId == (int)GlobalEnumVars.ActivityEnum.特价);
                where1 = where1.And((g, p) => p.activityTypeId == (int)GlobalEnumVars.ActivityEnum.特价);
                where2 = where2.And((g, p) => p.activityTypeId == (int)GlobalEnumVars.ActivityEnum.特价);
                where3 = where3.And((g, p) => p.activityTypeId == (int)GlobalEnumVars.ActivityEnum.特价);

                //2、审核状态
                where0 = where0.And(p => p.status == (int)GlobalEnumVars.ActivityStatus.Valid);
                where1 = where1.And((g, p) => p.status == (int)GlobalEnumVars.ActivityStatus.Valid);
                where2 = where2.And((g, p) => p.status == (int)GlobalEnumVars.ActivityStatus.Valid);
                where3 = where3.And((g, p) => p.status == (int)GlobalEnumVars.ActivityStatus.Valid);

                //3、有效期范围
                #region
                //string dayOfWeek = ((int)DateTime.Now.DayOfWeek).ToString();//星期选择
                string dayOfWeek = (((int)DateTime.Now.DayOfWeek - 1 + 7) % 7 + 1).ToString();

                string dayOfMonth = DateTime.Now.Day.ToString("d2"); //按月的某天选择
                TimeSpan timeOfDay = DateTime.Now.TimeOfDay;
                where0 = where0.And(p => p.activityBeginDate <= DateTime.Now && p.activityEndDate >= DateTime.Now);
                where0 = where0.And(p => p.activityBeginTime <= timeOfDay && p.activityEndTime >= timeOfDay);
                where0 = where0.And(p => p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtTimeRange
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtEveryDay)
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtWeekDay && p.weekdays.Contains(dayOfWeek))
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtMonthDay && p.weekdays.Contains(dayOfMonth))
                    );

                where1 = where1.And((g, p) => p.activityBeginDate <= DateTime.Now && p.activityEndDate >= DateTime.Now);
                where1 = where1.And((g, p) => p.activityBeginTime <= timeOfDay && p.activityEndTime >= timeOfDay);
                where1 = where1.And((g, p) => p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtTimeRange
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtEveryDay)
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtWeekDay && p.weekdays.Contains(dayOfWeek))
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtMonthDay && p.weekdays.Contains(dayOfMonth))
                    );

                where2 = where2.And((g, p) => p.activityBeginDate <= DateTime.Now && p.activityEndDate >= DateTime.Now);
                where2 = where2.And((g, p) => p.activityBeginTime <= timeOfDay && p.activityEndTime >= timeOfDay);
                where2 = where2.And((g, p) => p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtTimeRange
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtEveryDay)
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtWeekDay && p.weekdays.Contains(dayOfWeek))
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtMonthDay && p.weekdays.Contains(dayOfMonth))
                    );

                where3 = where3.And((g, p) => p.activityBeginDate <= DateTime.Now && p.activityEndDate >= DateTime.Now);
                where3 = where3.And((g, p) => p.activityBeginTime <= timeOfDay && p.activityEndTime >= timeOfDay);
                where3 = where3.And((g, p) => p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtTimeRange
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtEveryDay)
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtWeekDay && p.weekdays.Contains(dayOfWeek))
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtMonthDay && p.weekdays.Contains(dayOfMonth))
                    );
                #endregion

                //4、适用渠道
                where0 = where0.And(p => p.channelId == (int)GlobalEnumVars.ActivityChannel.All || p.channelId == (int)GlobalEnumVars.ActivityChannel.POS);
                where1 = where1.And((g, p) => p.channelId == (int)GlobalEnumVars.ActivityChannel.All || p.channelId == (int)GlobalEnumVars.ActivityChannel.POS);
                where2 = where2.And((g, p) => p.channelId == (int)GlobalEnumVars.ActivityChannel.All || p.channelId == (int)GlobalEnumVars.ActivityChannel.POS);
                where3 = where3.And((g, p) => p.channelId == (int)GlobalEnumVars.ActivityChannel.All || p.channelId == (int)GlobalEnumVars.ActivityChannel.POS);

                //5、促销人群
                if (member != null)
                {
                    where0 = where0.And(p => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Member);
                    where0 = where0.And(p => p.vipTypeIds == string.Empty || p.vipTypeIds.Contains(member.grade.ToString()));

                    where1 = where1.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Member);
                    where1 = where1.And((g, p) => p.vipTypeIds == string.Empty || p.vipTypeIds.Contains(member.grade.ToString()));

                    where2 = where2.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Member);
                    where2 = where2.And((g, p) => p.vipTypeIds == string.Empty || p.vipTypeIds.Contains(member.grade.ToString()));

                    where3 = where3.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Member);
                    where3 = where3.And((g, p) => p.vipTypeIds == string.Empty || p.vipTypeIds.Contains(member.grade.ToString()));
                }
                else
                {
                    where0 = where0.And(p => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                        || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Ordinary);
                    where1 = where1.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                        || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Ordinary);
                    where2 = where2.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                        || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Ordinary);
                    where3 = where3.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                        || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Ordinary);
                }

                //var activitymaster1 = new activitymasterServices().AsQueryable().Where(where0.ToExpression()).ToPageList(1, 1).FirstOrDefault();
                //var msql1 = new activitymasterServices().AsQueryable().Where(where0.ToExpression()).ToSqlString();

                //5、商品范围
                where0 = where0.And(p => p.goodsType == (int)GlobalEnumVars.ActivityGoodsType.All);
                where1 = where1.And((g, p) => g.pdtSpuSpecId == ordering.foodSpecId);

                if ((goodsTypeIds != null) && (goodsTypeIds.Count > 0))
                {
                    where2 = where2.And((g, p) => goodsTypeIds.Contains(g.itemSortId));
                    var goodsTypeList = new activitysortServices().AsQueryable()
                        .LeftJoin<activitymaster>((g, p) => g.activityInfoId == p.activityInfoId)
                        .WhereIF(true, where2.ToExpression()).ToList();

                    //5.1排除的分类
                    excGoodsSortIds = goodsTypeList.Where(p=>p.excludeFLag == true).Select(p => p.activityInfoId).ToList().Distinct().ToList();

                    if (excGoodsSortIds.Count > 0)
                        where2 = where2.And((g, p) => !excGoodsSortIds.Contains(g.activityInfoId));                
                    else
                        where2 = where2.And((g, p) => goodsTypeIds.Contains(g.itemSortId));
                }                   
                else
                    where2 = where2.And((g, p) => g.itemSortId == 0);

                where3 = where3.And((g, p) => g.pdtSpuSpecId == ordering.foodSpecId);

                //var msql1 = new activitysortServices().AsQueryable()
                //    .LeftJoin<activitymaster>((g, p) => g.activityInfoId == p.activityInfoId)
                //    .WhereIF(true, where2.ToExpression())
                //    .Select((g, p) => new activitysort
                //    {
                //    activityGoodsSortId = g.activityGoodsSortId,
                //    activityInfoId = p.activityInfoId,
                //    activityTypeId = p.activityTypeId,
                //    activitySubTypeId = p.activitySubTypeId,
                //    itemSortId = g.itemSortId,
                //    qty = g.qty,
                //    price = g.price,
                //    limitQty = g.limitQty,
                //    lineStatus = g.lineStatus,
                //    excludeFLag = g.excludeFLag,
                //    }).ToSqlString();

                //5.1、排除的商品
                var excGoodsIds = new activitygoodsexcludeServices().AsQueryable()
                    .LeftJoin<activitymaster>((g, p) => g.activityInfoId == p.activityInfoId)
                    .WhereIF(true, where3.ToExpression()).Select((g, p) => p.activityInfoId).ToList();

                if (excGoodsIds.Count > 0)
                {
                    where0 = where0.And(p => !excGoodsIds.Contains(p.activityInfoId));
                    where1 = where1.And((g, p) => !excGoodsIds.Contains(g.activityInfoId));
                    where2 = where2.And((g, p) => !excGoodsIds.Contains(g.activityInfoId));//2025-09-08
                }                   
                
                //5.2、排除的分类
                if (excGoodsSortIds.Count > 0)
                {
                    where0 = where0.And(p => !excGoodsSortIds.Contains(p.activityInfoId));
                    where1 = where1.And((g, p) => !excGoodsSortIds.Contains(g.activityInfoId));
                }                    

                //6.A、所有商品的促销
                var activitymaster = new activitymasterServices().AsQueryable().Where(where0.ToExpression())
                    .OrderBy(p => p.activityInfoId, OrderByType.Desc).ToPageList(1, 1).FirstOrDefault();

                if (activitymaster != null)
                {
                    ActivityItemDto itemDto = new ActivityItemDto();
                    itemDto.activityInfoId = activitymaster.activityInfoId;
                    itemDto.activityTypeId = activitymaster.activityTypeId;
                    //itemDto.activityValue = activitymaster.discountRate * 10;
                    itemDto.activityValue = activitymaster.discountRate;
                    activityList.Add(itemDto);
                }

                //6.B、指定商品的促销
                var activitygoods = base.AsQueryable().LeftJoin<activitymaster>((g, p) => g.activityInfoId == p.activityInfoId)
                    .WhereIF(true, where1.ToExpression())
                    .Select((g, p) => new activitygoods
                    {
                        activityGoodsId = g.activityGoodsId,
                        activityTypeId = p.activityTypeId,
                        activityInfoId = p.activityInfoId,
                        activitySubTypeId = p.activitySubTypeId,
                        spuId = g.spuId,
                        pdtSpuSpecId = g.pdtSpuSpecId,
                        qty = g.qty,
                        price = g.price,
                        saPrice = g.saPrice,
                        poPrice = g.poPrice,
                        limitQty = g.limitQty,
                        lineStatus = g.lineStatus,
                    }).OrderBy(g => g.activityInfoId, OrderByType.Desc)
                    .ToPageList(1, 1).FirstOrDefault();

                if (activitygoods != null)
                {
                    activityList.Add( new ActivityItemDto {
                        activityInfoId = activitygoods.activityInfoId,
                        activityTypeId = activitygoods.activityTypeId,
                        activityValue = activitygoods.price.ObjToDecimal(),
                    });
                }

                //6.C、指定分类的促销
                var activitysort = new activitysortServices().AsQueryable()
                    .LeftJoin<activitymaster>((g, p) => g.activityInfoId == p.activityInfoId)
                    .WhereIF(true, where2.ToExpression())
                    .Select((g, p) => new activitysort
                    {
                        activityGoodsSortId = g.activityGoodsSortId,
                        activityInfoId = p.activityInfoId,
                        activityTypeId = p.activityTypeId,
                        activitySubTypeId = p.activitySubTypeId,
                        itemSortId = g.itemSortId,
                        qty = g.qty,
                        price = g.price,
                        limitQty = g.limitQty,
                        lineStatus = g.lineStatus,
                        excludeFLag = g.excludeFLag,
                    }).OrderBy(g => g.activityInfoId, OrderByType.Desc)
                    .ToPageList(1, 1).FirstOrDefault();

                if (activitysort != null)
                {
                    activityList.Add(new ActivityItemDto
                    {
                        activityInfoId = activitysort.activityInfoId,
                        activityTypeId = activitysort.activityTypeId,
                        activityValue = activitysort.price.ObjToDecimal(),
                    });
                }
                if (activityList.Count == 0) return null;//没有查找到促销

                var ActivityMasterMax = activityList.OrderByDescending(x => x).ToList().FirstOrDefault(); // 降序

                activity = new ActivityDto();              
                
                activity.master = new activitymasterServices().GetById(ActivityMasterMax.activityInfoId);
                if (activity.master == null) return null;

                if (activity.master.activitySubTypeId == (int)GlobalEnumVars.ActivitySubType.限时特价)
                {
                    ordering.realPrice = ActivityMasterMax.activityValue;
                }
                else
                {
                    ordering.realPrice = ordering.price * ActivityMasterMax.activityValue / 10;
                }

                ordering.realAmount = ordering.realPrice * ordering.quantity;
                ordering.allAmount = ordering.realAmount + ordering.feedAmount + ordering.taskAmount;
                if (ordering.price != 0)
                    ordering.discount = ordering.realPrice / ordering.price;
                ordering.dataType = (int)GlobalEnumVars.OrderingDataType.ActivityDis;                
                
                activity.sort = activitysort;
                activity.goods = activitygoods;
                if ((activity.master != null) && (activity.master.limitBuyType == (int)GlobalEnumVars.LimitBuyType.LimitedQty))
                {
                    ordering.limitBuyQty = activity.master.limitBuyQty;//限购数量
                }
            }
            catch (Exception) { }
            return activity;
        }

        /// <summary>
        /// 促销计算(第二件半价与第二件0元计算）
        /// </summary>
        public ActivityDto SetOrderingTheSecondPiece(corecmsfoodordering ordering, corecmsuser member)
        {            
            List<ActivityItemDto> activityList = new List<ActivityItemDto>();
            List<long> excGoodsSortIds = new List<long>();
            var goodsTypeIds = new corecmsfoodcategoryServices().GetParentIds(ordering.foodTypeId);
            try
            {
                var where0 = Expressionable.Create<activitymaster>();
                var where1 = Expressionable.Create<activitygoods, activitymaster>();
                var where2 = Expressionable.Create<activitysort, activitymaster>();
                var where3 = Expressionable.Create<activitygoodsexclude, activitymaster>();

                //1、活动类型
                where0 = where0.And(p => p.activityTypeId == (int)GlobalEnumVars.ActivityEnum.第N件商品);
                where1 = where1.And((g, p) => p.activityTypeId == (int)GlobalEnumVars.ActivityEnum.第N件商品);
                where2 = where2.And((g, p) => p.activityTypeId == (int)GlobalEnumVars.ActivityEnum.第N件商品);
                where3 = where3.And((g, p) => p.activityTypeId == (int)GlobalEnumVars.ActivityEnum.第N件商品);

                //2、审核状态
                where0 = where0.And(p => p.status == (int)GlobalEnumVars.ActivityStatus.Valid);
                where1 = where1.And((g, p) => p.status == (int)GlobalEnumVars.ActivityStatus.Valid);
                where2 = where2.And((g, p) => p.status == (int)GlobalEnumVars.ActivityStatus.Valid);
                where3 = where3.And((g, p) => p.status == (int)GlobalEnumVars.ActivityStatus.Valid);

                //3、有效期范围
                #region
                //string dayOfWeek = ((int)DateTime.Now.DayOfWeek).ToString();//星期选择
                string dayOfWeek = (((int)DateTime.Now.DayOfWeek - 1 + 7) % 7 + 1).ToString();
                string dayOfMonth = DateTime.Now.Day.ToString("d2"); //按月的某天选择
                TimeSpan timeOfDay = DateTime.Now.TimeOfDay;
                where0 = where0.And(p => p.activityBeginDate <= DateTime.Now && p.activityEndDate >= DateTime.Now);
                where0 = where0.And(p => p.activityBeginTime <= timeOfDay && p.activityEndTime >= timeOfDay);
                where0 = where0.And(p => p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtTimeRange
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtEveryDay)
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtWeekDay && p.weekdays.Contains(dayOfWeek))
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtMonthDay && p.weekdays.Contains(dayOfMonth))
                    );

                where1 = where1.And((g, p) => p.activityBeginDate <= DateTime.Now && p.activityEndDate >= DateTime.Now);
                where1 = where1.And((g, p) => p.activityBeginTime <= timeOfDay && p.activityEndTime >= timeOfDay);
                where1 = where1.And((g, p) => p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtTimeRange
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtEveryDay)
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtWeekDay && p.weekdays.Contains(dayOfWeek))
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtMonthDay && p.weekdays.Contains(dayOfMonth))
                    );

                where2 = where2.And((g, p) => p.activityBeginDate <= DateTime.Now && p.activityEndDate >= DateTime.Now);
                where2 = where2.And((g, p) => p.activityBeginTime <= timeOfDay && p.activityEndTime >= timeOfDay);
                where2 = where2.And((g, p) => p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtTimeRange
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtEveryDay)
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtWeekDay && p.weekdays.Contains(dayOfWeek))
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtMonthDay && p.weekdays.Contains(dayOfMonth))
                    );

                where3 = where3.And((g, p) => p.activityBeginDate <= DateTime.Now && p.activityEndDate >= DateTime.Now);
                where3 = where3.And((g, p) => p.activityBeginTime <= timeOfDay && p.activityEndTime >= timeOfDay);
                where3 = where3.And((g, p) => p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtTimeRange
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtEveryDay)
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtWeekDay && p.weekdays.Contains(dayOfWeek))
                    || (p.weekdayType == (int)GlobalEnumVars.ActivityWeekdayType.AtMonthDay && p.weekdays.Contains(dayOfMonth))
                    );
                #endregion

                //4、适用渠道
                where0 = where0.And(p => p.channelId == (int)GlobalEnumVars.ActivityChannel.All || p.channelId == (int)GlobalEnumVars.ActivityChannel.POS);
                where1 = where1.And((g, p) => p.channelId == (int)GlobalEnumVars.ActivityChannel.All || p.channelId == (int)GlobalEnumVars.ActivityChannel.POS);
                where2 = where2.And((g, p) => p.channelId == (int)GlobalEnumVars.ActivityChannel.All || p.channelId == (int)GlobalEnumVars.ActivityChannel.POS);
                where3 = where3.And((g, p) => p.channelId == (int)GlobalEnumVars.ActivityChannel.All || p.channelId == (int)GlobalEnumVars.ActivityChannel.POS);

                //5、促销人群
                if (member != null)
                {
                    where0 = where0.And(p => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                          || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Member);

                    where0 = where0.And(p => p.vipTypeIds == string.Empty || p.vipTypeIds.Contains(member.grade.ToString()));

                    where1 = where1.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                          || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Member);
                    where2 = where2.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                          || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Member);
                    where3 = where3.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                          || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Member);
                }
                else
                {
                    where0 = where0.And(p => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                        || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Ordinary);
                    where1 = where1.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                        || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Ordinary);
                    where2 = where2.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                        || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Ordinary);
                    where3 = where3.And((g, p) => p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.All
                        || p.onlyVip == (int)GlobalEnumVars.ActivityOnlyVip.Ordinary);
                }

                //5、商品范围
                where0 = where0.And(p => p.goodsType == (int)GlobalEnumVars.ActivityGoodsType.All);
                where1 = where1.And((g, p) => g.pdtSpuSpecId == ordering.foodSpecId);
                if ((goodsTypeIds != null) && (goodsTypeIds.Count > 0))
                {
                    //5.1排除的分类
                    var where22 = where2.And((g, p) => goodsTypeIds.Contains(g.itemSortId) && g.excludeFLag == true);
                    excGoodsSortIds = new activitysortServices().AsQueryable()
                        .LeftJoin<activitymaster>((g, p) => g.activityInfoId == p.activityInfoId)
                        .WhereIF(true, where22.ToExpression()).Select((g, p) => p.activityInfoId).ToList().Distinct().ToList();

                    if (excGoodsSortIds.Count > 0)
                    {
                        where2 = where2.And((g, p) => !excGoodsSortIds.Contains(g.activityInfoId));
                    }
                    else
                        where2 = where2.And((g, p) => goodsTypeIds.Contains(g.itemSortId) && g.excludeFLag == true);
                }
                else
                    where2 = where2.And((g, p) => g.itemSortId == 0);

                where3 = where3.And((g, p) => g.pdtSpuSpecId == ordering.foodSpecId);

                //5、商品范围
                where0 = where0.And(p => p.goodsType == (int)GlobalEnumVars.ActivityGoodsType.All);
                where1 = where1.And((g, p) => g.pdtSpuSpecId == ordering.foodSpecId);

                if ((goodsTypeIds != null) && (goodsTypeIds.Count > 0))
                {
                    //5.1排除的分类
                    var where22 = where2.And((g, p) => goodsTypeIds.Contains(g.itemSortId) && g.excludeFLag == true);
                    excGoodsSortIds = new activitysortServices().AsQueryable()
                        .LeftJoin<activitymaster>((g, p) => g.activityInfoId == p.activityInfoId)
                        .WhereIF(true, where22.ToExpression()).Select((g, p) => p.activityInfoId).ToList().Distinct().ToList();

                    if (excGoodsSortIds.Count > 0)
                    {
                        //where1 = where1.And((g, p) => g == ordering.foodSpecId);
                        where2 = where2.And((g, p) => !excGoodsSortIds.Contains(g.activityInfoId));
                    }
                    else
                        where2 = where2.And((g, p) => goodsTypeIds.Contains(g.itemSortId) && g.excludeFLag == true);
                }
                else
                    where2 = where2.And((g, p) => g.itemSortId == 0);

                where3 = where3.And((g, p) => g.pdtSpuSpecId == ordering.foodSpecId);

                //5.1、排除的商品
                var excGoodsIds = new activitygoodsexcludeServices().AsQueryable()
                    .LeftJoin<activitymaster>((g, p) => g.activityInfoId == p.activityInfoId)
                    .WhereIF(true, where3.ToExpression()).Select((g, p) => p.activityInfoId).ToList();

                if (excGoodsIds.Count > 0)
                {
                    where0 = where0.And(p => !excGoodsIds.Contains(p.activityInfoId));
                    where1 = where1.And((g, p) => !excGoodsIds.Contains(g.activityInfoId));
                }

                //5.2、排除的分类
                if (excGoodsSortIds.Count > 0)
                {
                    where0 = where0.And(p => !excGoodsSortIds.Contains(p.activityInfoId));
                    where1 = where1.And((g, p) => !excGoodsSortIds.Contains(g.activityInfoId));
                }

                //6、星期选择
                //string dateNum =((int)DateTime.Now.DayOfWeek).ToString();
                //where = where.And(p => p.weekValues.Contains(dateNum));

                //7.A、所有商品的促销
                var activitymaster = new activitymasterServices().AsQueryable().Where(where0.ToExpression())
                    .OrderBy(p => p.activityInfoId, OrderByType.Desc).ToPageList(1, 1).FirstOrDefault();
                if (activitymaster != null)
                {
                    ActivityItemDto itemDto = new ActivityItemDto();
                    itemDto.activityInfoId = activitymaster.activityInfoId;
                    itemDto.activityTypeId = activitymaster.activityTypeId;
                    itemDto.activityValue = (decimal)0.5;//第二件半价
                    if (activitymaster.activitySubTypeId == (int)GlobalEnumVars.ActivitySubType.第二件0元)
                        itemDto.activityValue = 0;//第二件零元
                    activityList.Add(itemDto);
                }

                //7.B、指定商品的促销
                var activitygoods = base.AsQueryable().LeftJoin<activitymaster>((g, p) => g.activityInfoId == p.activityInfoId)
                    .WhereIF(true, where1.ToExpression())
                    .Select((g, p) => new activitygoods
                    {
                        activityGoodsId = g.activityGoodsId,
                        activityTypeId = p.activityTypeId,
                        activityInfoId = p.activityInfoId,
                        activitySubTypeId = p.activitySubTypeId,
                        spuId = g.spuId,
                        pdtSpuSpecId = g.pdtSpuSpecId,
                        qty = g.qty,
                        price = g.price,
                        saPrice = g.saPrice,
                        poPrice = g.poPrice,
                        limitQty = g.limitQty,
                        lineStatus = g.lineStatus,
                    }).OrderBy(g => g.activityInfoId, OrderByType.Desc)
                    .ToPageList(1, 1).FirstOrDefault();

                if (activitygoods != null)
                {
                    ActivityItemDto itemDto = new ActivityItemDto();
                    itemDto.activityInfoId = activitygoods.activityInfoId;
                    itemDto.activityTypeId = activitygoods.activityTypeId;
                    itemDto.activityValue = (decimal)0.5;//第二件半价
                    if (activitygoods.activitySubTypeId == (int)GlobalEnumVars.ActivitySubType.第二件0元)
                        itemDto.activityValue = 0;//第二件零元
                    activityList.Add(itemDto);
                }

                //7.C、指定分类的促销
                var activitysort = new activitysortServices().AsQueryable().LeftJoin<activitymaster>((g, p) => g.activityInfoId == p.activityInfoId)
                 .WhereIF(true, where2.ToExpression())
                 .Select((g, p) => new activitysort
                 {
                     activityGoodsSortId = g.activityGoodsSortId,
                     activityInfoId = p.activityInfoId,
                     activityTypeId = p.activityTypeId,
                     activitySubTypeId = p.activitySubTypeId,
                     itemSortId = g.itemSortId,
                     qty = g.qty,
                     price = g.price,
                     limitQty = g.limitQty,
                     lineStatus = g.lineStatus,
                     excludeFLag = g.excludeFLag,
                 }).OrderBy(g => g.activityInfoId, OrderByType.Desc)
                 .ToPageList(1, 1).FirstOrDefault();

                if (activitysort != null)
                {
                    ActivityItemDto itemDto = new ActivityItemDto();
                    itemDto.activityInfoId = activitysort.activityInfoId;
                    itemDto.activityTypeId = activitysort.activityTypeId;
                    itemDto.activityValue = (decimal)0.5;//第二件半价
                    if (activitysort.activitySubTypeId == (int)GlobalEnumVars.ActivitySubType.第二件0元)
                        itemDto.activityValue = 0;//第二件零元
                    activityList.Add(itemDto);
                }
                if (activityList.Count == 0) return null;//没有查找到促销

                var ActivityMasterMax = activityList.OrderByDescending(x => x).ToList().FirstOrDefault(); // 降序

                ActivityDto activity = new ActivityDto();

                activity.master = new activitymasterServices().GetById(ActivityMasterMax.activityInfoId);
                if (activity.master == null) return null;

                activity.master.discountRate = ActivityMasterMax.activityValue;
                activity.sort = activitysort;
                activity.goods = activitygoods;
                return activity;
            }
            catch (Exception) { }
            return null;
        }

        #endregion

    }
}